package org.hamster.multidropbox.mainView.view
{
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	import mx.core.IFlexDisplayObject;
	import mx.managers.PopUpManager;
	
	import org.hamster.common.puremvc.mediator.BaseMediator;
	import org.hamster.dropbox.DropboxClient;
	import org.hamster.dropbox.DropboxEvent;
	import org.hamster.dropbox.models.AccountInfo;
	import org.hamster.multidropbox.dropbox.constants.AppDropboxClientStatus;
	import org.hamster.multidropbox.dropbox.vo.AppDropboxClient;
	import org.hamster.multidropbox.main.utils.AppUtil;
	import org.hamster.multidropbox.main.utils.PureMVCHelper;
	import org.hamster.multidropbox.mainView.event.MainViewEvent;
	
	public class ImportAccountPanelMediator extends BaseMediator
	{
		public static const NAME:String = "org.hamster.multidropbox.mainView.view.ImportAccountPanelMediator";
		
		private var dropboxClient:AppDropboxClient;
		
		public function ImportAccountPanelMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
		}
		
		override public function onRegister():void
		{
			
		}
		
		override public function onRemove():void
		{
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				MainViewEvent.IMPORT_ACCOUNT_POPUP_SHOW,
				MainViewEvent.IMPORT_ACCOUNT_POPUP_HIDE
			];
		}
		
		public function handleImportAccountPopupShow(body:Object):void
		{
			viewComponent = AppUtil.popup(ImportAccountPanel);
			app.addBtn.addEventListener(MouseEvent.CLICK, addBtnClickHandler);
			app.cancelBtn.addEventListener(MouseEvent.CLICK, cancelBtnClickHandler);
			app.retryRequestTokenBtn.addEventListener(MouseEvent.CLICK, requestToken);
			app.authenticationBtn.addEventListener(MouseEvent.CLICK, authentication);
			app.accessTokenBtn.addEventListener(MouseEvent.CLICK, accessToken);
			
			requestToken();
		}
		
		public function handleImportAccountPopupHide(body:Object):void
		{
			PopUpManager.removePopUp(viewComponent as IFlexDisplayObject);
			app.addBtn.removeEventListener(MouseEvent.CLICK, addBtnClickHandler);
			app.cancelBtn.removeEventListener(MouseEvent.CLICK, cancelBtnClickHandler);
			app.retryRequestTokenBtn.removeEventListener(MouseEvent.CLICK, requestToken);
			app.authenticationBtn.removeEventListener(MouseEvent.CLICK, authentication);
			app.accessTokenBtn.removeEventListener(MouseEvent.CLICK, accessToken);
			this.viewComponent = null;
		}
		
		private function requestToken(event:MouseEvent = null):void
		{
			app.requestTokenLoading = true;
			app.retryRequestToken   = false;
			dropboxClient = PureMVCHelper.dropboxClientListProxy.genClient();
			dropboxClient.addEventListener(DropboxEvent.REQUEST_TOKEN_RESULT, requestTokenResultHandler);
			dropboxClient.addEventListener(DropboxEvent.REQUEST_TOKEN_FAULT,  requestTokenFaultHandler);
			dropboxClient.requestToken();
		}
		
		private function authentication(event:MouseEvent):void
		{
			navigateToURL(new URLRequest(dropboxClient.authorizationUrl));
			app.accessToken = true;
			app.statusLbl.text = AppUtil.getStrMainView('importAccountPanel.accessTokenTip');
		}
		
		private function accessToken(event:MouseEvent = null):void
		{
			app.accessTokenLoading = true;
			dropboxClient.addEventListener(DropboxEvent.ACCESS_TOKEN_RESULT, accessTokenResultHandler);
			dropboxClient.addEventListener(DropboxEvent.ACCESS_TOKEN_FAULT,  accessTokenFaultHandler);
			dropboxClient.accessToken();
		}
		
		private function accountInfo():void
		{
			app.accountInfoLoading = true;
			dropboxClient.addEventListener(DropboxEvent.ACCOUNT_INFO_RESULT, accountInfoResultHandler);
			dropboxClient.addEventListener(DropboxEvent.ACCOUNT_INFO_FAULT,  accountInfoFaultHandler);
			dropboxClient.accountInfo();
		}
		
		private function requestTokenResultHandler(event:DropboxEvent):void
		{
			app.requestTokenLoading = false;
			app.retryRequestToken   = false;
			dropboxClient.removeEventListener(DropboxEvent.REQUEST_TOKEN_RESULT, requestTokenResultHandler);
			dropboxClient.removeEventListener(DropboxEvent.REQUEST_TOKEN_FAULT,  requestTokenFaultHandler);
			app.statusLbl.text = AppUtil.getStrMainView('importAccountPanel.requestTokenSuccess');
			
			app.requestTokenKeyTI.text    = dropboxClient.config.requestTokenKey;
			app.requestTokenSecretTI.text = dropboxClient.config.requestTokenSecret;
			app.authentication            = true;
		}
		
		private function requestTokenFaultHandler(event:DropboxEvent):void
		{
			app.requestTokenLoading = false;
			app.retryRequestToken   = true;
			dropboxClient.removeEventListener(DropboxEvent.REQUEST_TOKEN_RESULT, requestTokenResultHandler);
			dropboxClient.removeEventListener(DropboxEvent.REQUEST_TOKEN_FAULT,  requestTokenFaultHandler);
			app.statusLbl.text = AppUtil.getStrMainView('importAccountPanel.requestTokenFailed');
		}
		
		private function accessTokenResultHandler(event:DropboxEvent):void
		{
			app.accessTokenLoading = false;
			app.accessTokenKeyTI.text    = dropboxClient.config.accessTokenKey;
			app.accessTokenSecretTI.text = dropboxClient.config.accessTokenSecret;
			dropboxClient.removeEventListener(DropboxEvent.ACCESS_TOKEN_RESULT, accessTokenResultHandler);
			dropboxClient.removeEventListener(DropboxEvent.ACCESS_TOKEN_FAULT,  accessTokenFaultHandler);
			accountInfo();
			
		}
		
		private function accessTokenFaultHandler(event:DropboxEvent):void
		{
			app.accessTokenLoading = false;
			dropboxClient.removeEventListener(DropboxEvent.ACCESS_TOKEN_RESULT, accessTokenResultHandler);
			dropboxClient.removeEventListener(DropboxEvent.ACCESS_TOKEN_FAULT,  accessTokenFaultHandler);
			app.statusLbl.text = AppUtil.getStrMainView('importAccountPanel.accessTokenFailed');
		}
		
		private function accountInfoResultHandler(event:DropboxEvent):void
		{
			app.authentication 		= false;
			app.accessToken 		= false;
			app.accountInfoLoading 	= false;
			dropboxClient.removeEventListener(DropboxEvent.ACCOUNT_INFO_RESULT, accountInfoResultHandler);
			dropboxClient.removeEventListener(DropboxEvent.ACCESS_TOKEN_FAULT,  accountInfoFaultHandler);
			dropboxClient.accountDetail = event.resultObject as AccountInfo;
			app.accountIdTI.text = this.dropboxClient.accountDetail.uid.toString();
			
			for each (var account:AppDropboxClient in PureMVCHelper.dropboxClientListProxy.dropboxClientList) {
				if (account.accountDetail.uid == dropboxClient.accountDetail.uid) {
					app.statusLbl.text = AppUtil.getStrMainView('importAccountPanel.accountDuplicated');
					return;
				}
			}
			app.canAddNow          = true;
			app.statusLbl.text   = AppUtil.getStrMainView('importAccountPanel.accessTokenSuccess');
			
			
		}
		
		private function accountInfoFaultHandler(event:DropboxEvent):void
		{
			app.accountInfoLoading = false;
			dropboxClient.removeEventListener(DropboxEvent.ACCOUNT_INFO_RESULT, accountInfoResultHandler);
			dropboxClient.removeEventListener(DropboxEvent.ACCESS_TOKEN_FAULT,  accountInfoFaultHandler);
		}
		
		private function addBtnClickHandler(event:MouseEvent):void
		{
			dropboxClient.status = AppDropboxClientStatus.AVAILABLE;
			PureMVCHelper.dropboxClientListProxy.dropboxClientList.addItem(dropboxClient);
			
			PureMVCHelper.dropboxStorageProxy.saveDropboxStorage();
			cancelBtnClickHandler(event);
		}
		
		private function cancelBtnClickHandler(event:MouseEvent):void
		{
			sendNotification(MainViewEvent.IMPORT_ACCOUNT_POPUP_HIDE);
		}
		
		public function get app():ImportAccountPanel
		{
			return viewComponent as ImportAccountPanel;
		}
	}
}